package org.ricks.net.action.rpc;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * // 使用示例
 * public class AppInitializer {
 *     public static void init() {
 *         RpcMetrics clientMetrics = new RpcMetrics();
 *         RpcMetrics serverMetrics = new RpcMetrics();
 *
 *         RpcClient client = new RpcClient(session, parser, 5000, clientMetrics);
 *         RpcHandler.setMetrics(serverMetrics);
 *
 *         new RpcMonitor(clientMetrics, serverMetrics).startReport();
 *     }
 * }
 */
public class RpcMonitor {
    private final RpcMetrics clientMetrics;
    private final RpcMetrics serverMetrics;

    public RpcMonitor(RpcMetrics clientMetrics, RpcMetrics serverMetrics) {
        this.clientMetrics = clientMetrics;
        this.serverMetrics = serverMetrics;
    }

    // 定时输出监控报告
    public void startReport() {
        ScheduledExecutorService reporter = Executors.newSingleThreadScheduledExecutor();
        reporter.scheduleAtFixedRate(() -> {
            System.out.println("=== Client Metrics ===");
            clientMetrics.printStats();
            System.out.println("=== Server Metrics ===");
            serverMetrics.printStats();
        }, 1, 1, TimeUnit.MINUTES);
    }
}
